<?php

class Pais{
	public $Id;
	public $Nombre;
	
	function __construct($id,$nombre){
		$this->Id	=$id;
		$this->Nombre = $nombre;
	}
	
	public function Insertar(){
		global $dbh;
		$sql = 'INSERT INTO  `pais` (
				`IdPais` ,
				`NomPais`
				)
				VALUES (
				NULL ,
				\''.$this->nombre.'\'
				)';
				
		$temp_q_id = $dbh->prepare($sql);
		if (!$temp_q_id) {
				echo "\nPDO::errorInfo():\n";		
		}
		$temp_q_id->execute();
					
		$this->id = $dbh->lastInsertId();		
	}
	
	static function ListarPaises(){
		global $dbh;
		$dbh->query("SET NAMES 'utf8'");
		
		$sql = 'SELECT IdPais,NomPais FROM pais ORDER BY NomPais';
		$lista = array();
		 
		foreach($dbh->query($sql) as $row) {
			$lista[] = new Pais($row['IdPais'],$row['NomPais']);
		}
		
		return $lista;
	}
	
	static function Buscar($texto){
		global $dbh;
		
		$sql = 'SELECT `NomPais` FROM `pais` WHERE NomPais LIKE "'.$texto.'"';
		$resultado = $dbh->query($sql);
		
		if($resultado->rowCount() > 0){
			return true;	
		}
		else{
			return false;	
		}
		
	}
	
	static function Actualizar($id,$nombre){
		global $dbh;
		
		$sql = 'UPDATE `pais` SET
				`NomPais`=\''.$nombre.'\'  
				WHERE 
				`IdPais`=\''.$id.'\' ';
		
		$temp_q_id = $dbh->prepare($sql);
		if (!$temp_q_id) {
				echo "\nPDO::errorInfo():\n";		
		}
		$temp_q_id->execute();
		
	}
	
	static function Eliminar($id)
	{
		global $dbh;
		
		$sql = "Delete from pais where IdPais = ".$id;
		$temp_q_id = $dbh->prepare($sql);
		if (!$temp_q_id) {
			echo "\nPDO::errorInfo():\n";				
		}
		$temp_q_id->execute();		
	}
	
	static function Obtener($id){
		global $dbh;
		
		$sql = 'SELECT `IdPais`,`NomPais`  FROM  `pais`
				WHERE IdPais = '.$id;
								
		$pais = null;
		foreach($dbh->query($sql) as $row) {
			$pais = new Pais($row['IdPais'],$row['NomPais']);	
		}
		
		return $pais;
	}
}
?>